package com.hankol.winecellar.adapter

import android.graphics.Paint
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import cn.iwgang.countdownview.CountdownView
import com.baselibrary.MessageBus
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.utils.*
import com.baselibrary.view.RoundCornerImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.winecellar.R
import com.hankol.winecellar.bean.WineryGoodsSeckillBean
import com.baselibrary.utils.AmountUtil
import com.baselibrary.view.ProgressWithTextBar
import com.xinlan.imageeditlibrary.editimage.utils.DensityUtil

/**酒窖详情-秒杀商品
 * @author: merbng
 * @time: 2023/3/15 13:00
 * @description:
 */
class WinerySeckillAdapter : BaseQuickAdapter<WineryGoodsSeckillBean, BaseViewHolder>(R.layout.item_winery_goods_seckill) {

    override fun convert(helper: BaseViewHolder, item: WineryGoodsSeckillBean) {
        helper.apply {
            //是否显示视频标识
            setGone(R.id.img_is_video, item.hasVideos == 1)
            val progress = getView<ProgressWithTextBar>(R.id.progress)
            progress.setProgressColor(ContextCompat.getColor(mContext, R.color.color_D53))
            progress.setBgColor(ContextCompat.getColor(mContext, R.color.color_D53_a30))
            progress.setTextColor(ContextCompat.getColor(mContext, R.color.white))
            progress.setTextSize(8f)
//            if (helper.adapterPosition == 0) {
//                progress.setText("10")
//                progress.setProgress(0.1f)
//            } else if (helper.adapterPosition == 1) {
//                progress.setText("40")
//                progress.setProgress(0.4f)
//            } else if (helper.adapterPosition == 2) {
//                progress.setText("60")
//                progress.setProgress(0.6f)
//            } else if (helper.adapterPosition == 3) {
//                progress.setText("80")
//                progress.setProgress(0.7f)
//            } else if (helper.adapterPosition == 4) {
//                progress.setText("100")
//                progress.setProgress(0.8f)
//            } else if (helper.adapterPosition == 5) {
//                progress.setText("100")
//                progress.setProgress(1f)
//            } else {
//                progress.setText("0")
//                progress.setProgress(0f)
//            }

            if (item.spuIcon.checkValue().checkNotNull()) {
                getView<RoundCornerImageView>(R.id.img_cover).load(mContext, item.spuIcon.checkValue())
            }
            setText(R.id.tv_name, item.spuName.checkValue())
            item.seckillActProductExtDTO?.let {
                setText(R.id.tv_price, AmountUtil.changeF2Y(if (SpUtils.getInstance().isFans) it.fansPrice.toString() else it.price.toString()))
                setText(R.id.tv_orgin_price, "¥ ${AmountUtil.changeF2Y(if (SpUtils.getInstance().isFans) item.fansPrice.toString() else item.price.toString())}")
            }
            getView<TextView>(R.id.tv_orgin_price).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG)

            //右边距处理
            val rlRoot = getView<LinearLayout>(R.id.cv_root)
            val layoutParams = rlRoot.layoutParams as RecyclerView.LayoutParams
            layoutParams.rightMargin = DensityUtil.dip2px(mContext, if (helper.adapterPosition == data.size - 1) 14f else 0f)
            layoutParams.leftMargin = DensityUtil.dip2px(mContext, if (helper.adapterPosition == 0) 14f else 8f)
            rlRoot.layoutParams = layoutParams
            item.seckillActProductExtDTO?.let {
                val countdownView = getView<CountdownView>(R.id.countdownView)

                val tvOption = getView<TextView>(R.id.tv_option)
                if (TimeUtil.getCurrentTimeMillis().minus(item.stime) > 0) {//进行中
                    val difTime = item.etime.minus(TimeUtil.getCurrentTimeMillis())
                    countdownView.customTimeShow(difTime >= 1000 * 60 * 60 * 24, true, true, true, false)
                    countdownView.start(difTime)
                    countdownView.setOnCountdownEndListener {
                         remove(helper.adapterPosition)
                        if(data.size==0||data.size==1){
                            EventBusUtils.sendNotify(MessageBus.msgId_seckill_once_end)
                        }
                    }
                    //进行中   & 已抢光
                    progress.visibility = View.VISIBLE
                    setText(R.id.tv_time_tip, "时间还剩 ")
                    setGone(R.id.tv_subscribe_num, false)
                    val isOver = it.sales >= it.stock
                    tvOption.setText(if (isOver) "已抢光" else "抢购")
                    tvOption.setTextColor(ContextCompat.getColor(mContext, R.color.white))
                    tvOption.setBackgroundResource(if (isOver) R.drawable.bg_solid_f54d53_r4_a50 else R.drawable.bg_solid_f54d53_r4)
                    val progressStr = Math.floor(it?.sales?.div(item.seckillActProductExtDTO.stock?.toFloat()!!)?.times(100)!!.toDouble())
                    progress.setText("${CommonUtil.formatNum(progressStr.toFloat()).toInt()}")
                    val progressValue = it?.sales?.div(item.seckillActProductExtDTO.stock?.toFloat()!!)!!
                    progress.setProgressWithAnimation(progressValue)
                    progress.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
                        override fun onViewAttachedToWindow(v: View?) {
                            progress.setProgressWithAnimation(progressValue)
                        }

                        override fun onViewDetachedFromWindow(v: View?) {
                        }
                    })
                } else {//未开始
                    addOnClickListener(R.id.tv_option)
                    //差值
                    val difTime = item.stime.minus(TimeUtil.getCurrentTimeMillis())
                    countdownView.customTimeShow(difTime >= 1000 * 60 * 60 * 24, true, true, true, false)
                    countdownView.start(difTime)
                    countdownView.setOnCountdownEndListener {
                        notifyItemChanged(adapterPosition)
                    }
                    //未开始
                    progress.visibility = View.GONE
                    setText(R.id.tv_time_tip, "距活动开始 ")
                    setGone(R.id.tv_subscribe_num, true)
                    setText(R.id.tv_subscribe_num, "${it.subscribeNum}人已预约")
                    tvOption.setText(if (it.subscribeFlg == 1) "取消提醒" else "预约提醒")
                    tvOption.setTextColor(ContextCompat.getColor(mContext, if (it.subscribeFlg == 1) R.color.color_999999 else R.color.white))
                    tvOption.setBackgroundResource(if (it.subscribeFlg == 1) R.drawable.bg_solid_999999_r4_a10 else R.drawable.bg_solid_f54d53_r4)
                }
            }
        }
    }

}